﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Manage
{
    /// <summary>
    /// 页面业务类
    /// </summary>
    public class PageBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>页面集合</returns>
        public List<Page> Query(int page, int rows, ref int total)
        {
            List<Page> results = new List<Page>();

            PageHibernate hibernate = new PageHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(Page value)
        {
            bool result = false;

            PageHibernate hibernate = new PageHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Update(Page value)
        {
            bool result = false;

            PageHibernate hibernate = new PageHibernate();
            result = hibernate.Update(value);

            return result;
        }

        /// <summary>
        /// 同步
        /// </summary>
        /// <param name="values">所有页面</param>
        /// <param name="user">操作用户</param>
        /// <returns>结果</returns>
        public bool Sync(List<Page> values, User user)
        {
            bool result = true;

            int total = 0;
            List<Page> hasPages = this.Query(1, int.MaxValue, ref total);

            List<Page> deletePages = new List<Page>();
            List<Page> addPages = new List<Page>();

            bool has = false;
            for (int i = 0; i < hasPages.Count; i++)
            {
                has = false;
                for (int j = 0; j < values.Count; j++)
                {
                    if (hasPages[i].Name == values[j].Name)
                    {
                        has = true;
                        break;
                    }
                }
                if (!has)
                {
                    deletePages.Add(hasPages[i]);
                }
            }

            for (int i = 0; i < values.Count; i++)
            {
                has = false;
                for (int j = 0; j < hasPages.Count; j++)
                {
                    if (values[i].Name == hasPages[j].Name)
                    {
                        has = true;
                        break;
                    }
                }
                if (!has)
                {
                    addPages.Add(values[i]);
                }
            }

            PageHibernate pageHibernate = new PageHibernate();
            PostPageHibernate postPageHibernate = new PostPageHibernate();

            // 删除
            if (result)
            {
                for (int i = 0; i < deletePages.Count; i++)
                {
                    result = postPageHibernate.DeleteByPage(deletePages[i].Guid);
                    if (!result)
                    {
                        break;
                    }
                    result = pageHibernate.Delete(deletePages[i].Guid);
                    if (!result)
                    {
                        break;
                    }
                }
            }

            // 增加
            if (result)
            {
                for (int i = 0; i < addPages.Count; i++)
                {
                    DateTime now = DateTime.Now;

                    Page page = new Page();
                    page.InsertUserId = user.Guid;
                    page.InsertTime = now;
                    page.UpdateUserId = user.Guid;
                    page.UpdateTime = now;
                    page.Remark = string.Empty;
                    page.Validity = true;

                    page.Name = addPages[i].Name;

                    result = pageHibernate.Insert(page);
                    if (!result)
                    {
                        break;
                    }
                }
            }

            return result;
        }
    }
}
